package com.yu.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yu.bean.Comment;
import com.yu.bean.CommentExample;
import com.yu.bean.User;
import com.yu.bean.InfoExample.Criteria;
import com.yu.dao.CommentMapper;

@Service
public class CommentService {

	@Autowired
	CommentMapper commentMapper;

	/**
	 * 插入评论
	 * @param comment
	 * @param session
	 */
	public void addComment(Comment comment, HttpSession session) {
		// TODO Auto-generated method stub
		comment.setComDate(new Date());
		//插入之前判断用户是否登录
		User user = (User)session.getAttribute("u");
		if(user != null){
			comment.setUserId(user.getUserId());
		}else{
			comment.setUserId(0);
		}
		commentMapper.insert(comment);
		
	}

	/**
	 * 根据插入的评论Id 获取插入的评论信息
	 * @param comId
	 * @return
	 */
	public Comment getCommentById(Integer comId) {
		// TODO Auto-generated method stub
		Comment comment = commentMapper.selectByPrimaryKeyWidUserInfo(comId);
		return comment;
	}

	/**
	 * 获取所有评论内容根据InfoId
	 * @param infoId
	 * @return
	 */
	public List<Comment> getAllCommentByInfoId(Integer infoId) {
		// TODO Auto-generated method stub
		CommentExample commentExample = new CommentExample();
		commentExample.setOrderByClause("com_date DESC");
		com.yu.bean.CommentExample.Criteria criteria = commentExample.createCriteria();
		criteria.andInfoIdEqualTo(infoId);
		List<Comment> comments = commentMapper.selectByExampleWidthUserInfo(commentExample);
		return comments;
	}

	/**
	 * 根据批量要删除的信息删除对应的评论
	 * @param del_ids
	 */
	public void deleteCommentByInfoIds(List<Integer> del_ids) {
		// TODO Auto-generated method stub
		CommentExample commentExample = new CommentExample();
		com.yu.bean.CommentExample.Criteria criteria = commentExample.createCriteria();
		criteria.andInfoIdIn(del_ids);
		commentMapper.deleteByExample(commentExample);
		
	}

	/**
	 * 根据要删除的信息id 删除对应的评论
	 * @param id
	 */
	public void deletCommentByinfoId(Integer id) {
		// TODO Auto-generated method stub
		CommentExample commentExample = new CommentExample();
		com.yu.bean.CommentExample.Criteria criteria = commentExample.createCriteria();
		criteria.andDeleteInfoIdEqualTo(id);
		commentMapper.deleteByExample(commentExample);
		
	}

	/**
	 * 插入评论信息
	 * @param infoId 
	 * @param comContent
	 */
	/*public void addComment(Integer infoId, String comContent, HttpSession session) {
		// TODO Auto-generated method stub
		//新声生成一个插入对象
		Comment comment = new Comment();
		comment.setComContent(comContent);
		comment.setInfoId(infoId);
		comment.setComDate(new Date());
		//插入之前判断用户是否登录
		User user = (User)session.getAttribute("u");
		if(user != null){
			comment.setUserId(user.getUserId());
		}else{
			comment.setUserId(0);
		}
		commentMapper.insertSelective(comment);
		System.out.println("cha ru de 最新主键id wei=======>"+comment.getComId());
		
	}*/
}
